package com.yuanluludev.lib_logcat.logcat;

import android.app.Application;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.widget.Toast;

import com.yuanluludev.lib_logcat.crash.CrashHandler;


/**
 * desc   : Logcat 初始化
 */
public final class InitProvider extends ContentProvider {

    private Context mContext;

    @Override
    public boolean onCreate() {
        mContext = getContext();
        if (mContext != null) {
            LogcatConfig.init(mContext.getApplicationContext());
            if (mContext instanceof Application) {
//                boolean upload = MetaDataUtil.Companion.getBoolean("UPLOADLOGCAT", context);
                boolean isAppDebug = isAppDebug();
                if (isAppDebug) {
                    FloatingLifecycle.with((Application) mContext);
                } else {
//                    UMConfigure.init(context, MetaDataUtil.Companion.getString("LOGCATKEY",context), "umeng", UMConfigure.DEVICE_TYPE_PHONE, "");
//                    MobclickAgent.setCatchUncaughtExceptions(false);
                }
                // 本地异常捕捉
                CrashHandler.Companion.getInstance().init((Application) mContext, !isAppDebug);
            } else {
                Toast.makeText(mContext, "参数错误，无法启动 Logcat", Toast.LENGTH_LONG).show();
            }
        }
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        return null;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        return null;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        return 0;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        return 0;
    }

    private boolean isAppDebug() {
        try {
            PackageManager pm = mContext.getPackageManager();
            ApplicationInfo ai = pm.getApplicationInfo(mContext.getPackageName(), 0);
            return (ai.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return false;
        }
    }

}